<?php
namespace Admin\Controller;

use think\Controller;
use think\Verify;
class PublicController extends Controller
{
	/*	登陆方法
	*/
	public function login()
	{
		$this->display();//渲染模板
	}
 /**
     * 生成验证码
     */
 public function verify()
 {
 	$config = array(
 		'fontSize' => 15,
 		'length'   => 4,
 		'useNoise' => false,
 		'imageW'   => 108,
 		'imageH'   => 42,
 		'codeSet'  => '0123456789'
 	);
 	ob_clean();
 	$verify = new Verify($config);
 	$img=$verify->entry();
 	return $img;
 }
 /*检查登录信息*/
 public function checkLogin()
 {
 	/*检查验证码是否正确*/
 	$code = I('code');
 	$verify = $this->checkCode($code);
 	if(!$verify){
 		$res['status']  = 0;
 		$res['message'] = '验证码错误';
 		$this->ajaxReturn($res);
 	}
 	/*检测用户名密码是否正确*/
 	$username = I("username"," ","trim");
 	$password = I("password"," ","md5");
 	$return   = $this->checkPassword($username,$password);
 	if(!$return){
 		$res['status']  = 0;
 		$res['message'] = '用户名或者密码错误';
 		$this->ajaxReturn($res);
 	}else{
 		$data = array(
 			"loginip" => get_client_ip(),
 			"logintime" =>date("Y-m-d H:i:s"),
 		);
 		M('admin')->save($data);
 		session('admin_id',$return['id']);
 		session('admin_username',$return['username']);
 		$res['status'] = 1;
 		$res['message'] = '登录成功！';
 		$this->ajaxReturn($res);
 	}

 }

/* 检测用户输入验证码是否正确
 @param $code : 输入的验证码
 @return bool : 验证码正确返回true，否则返回false
 */
 public function checkCode($code)
 {
 	$verify = new Verify();
 	return $verify->check($code);
 }

/* 检测用户输入验证码是否正确
 @param $username
 @param $password
 @return bool
 */
 public function checkPassword($username,$password)
 {
 	$map['username'] = $username;
 	$admin = M('admin')->where($map)->find();
 	if($admin['password'] === $password){
 		return $admin;
 	}else{
 		return false;
 	}
 }

 // 退出
 public function logout()
 {
 	session('admin_id',null);
 	session('admin_username',mull);
 	$this->redirect("login");
 }
}